﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace SharePointDatabaseExplorer
{
    class Database
    {
        public static string _SQLSever = string.Empty;
        public static string _DatabaseName = string.Empty;
        public static string _UserID = string.Empty;
        public static string _Password = string.Empty;
        public static Boolean _IntegratedSecurity = false;
        public static string _ConnectionString = string.Empty;
        public static string _FileOutputPath = string.Empty;

        public string SQLServer
        {
            get
            {
                return _SQLSever;
            }
            set
            {
                _SQLSever = value;
            }
        }
        public string DatabaseName
        {
            get
            {
                return _DatabaseName;
            }
            set
            {
                _DatabaseName = value;
            }
        }
        public string UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }
        public Boolean IntegratedSecurity
        {
            get
            {
                return _IntegratedSecurity;
            }
            set
            {
                _IntegratedSecurity = value;
            }
        }
        public string ConnectionString
        {
            get
            {
                return _ConnectionString;
            }
            set
            {
                _ConnectionString = value;
            }
        }
        public string FileOutputPath
        {
            get
            {
                return _FileOutputPath;
            }
            set
            {
                _FileOutputPath = value;
            }
        }

        public static DataSet RunQuery(String Query)
        {
            DataSet ds = new DataSet();
            String connStr = Database._ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand objCommand = new SqlCommand(Query, conn);
                SqlDataAdapter da = new SqlDataAdapter(objCommand);
                da.Fill(ds);
                da.Dispose();
            }
            return ds;
        }

        public static SqlDataReader GetReader(string Query)
        {
            // Sharepoint content DB connection string
            string DBConnString = Database._ConnectionString;
            
            SqlConnection con = new SqlConnection(DBConnString);
            con.Open();

            SqlCommand com = con.CreateCommand();
            com.CommandText = Query;

            SqlDataReader reader = com.ExecuteReader();

            return reader;
        }

        public static string ParseColumn(string sColumnName)
        {
            string sReturn = string.Empty;
            if(sColumnName.StartsWith("$Resources"))
            {
                int iCoreVal = sColumnName.IndexOf("core,");
                sReturn = sColumnName.Substring(iCoreVal + 5,sColumnName.Length - 15);
            }
            else
            {
                sReturn = sColumnName;
            }

            return sReturn;
        }
    }
}
